package com.ys.controller.web.sys;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.bo.dto.NameValueSelectDto;
import com.bo.enume.sys.StatusEnum;
import com.ys.entity.sys.YsMenu;
import com.ys.enume.sys.YsMenuEnum;
import com.ys.mapper.sys.SysMapper;
import com.ys.service.sys.YsMenuService;
import com.ys.util.StringUtil;
import com.ys.vo.W;

import lombok.AllArgsConstructor;
import lombok.Data;

/**
 * 
 * @author Jason
 *
 */
@Controller
@RequestMapping("/web/sys/ysMenu")
public class WebYsMenuController {
	@Autowired
	private SysMapper sysMapper;
	
	@Autowired
	private YsMenuService ysMenuService;
	
	/**
	 * 新增
	 */
	@GetMapping("/toAdd")
	public String toAdd(Model model, Long pid){

		YsMenu pMenu = new YsMenu().setId(pid).queryFirst();
		if (pMenu.getLev() == YsMenuEnum.Lev.THREE.value) {
			return W.error(model, "三级菜单无法新增子菜单");
		}
		
		List<NameValueSelectDto> templetTypeArr = new ArrayList<NameValueSelectDto>();
		for (YsMenuEnum.TempletType e : YsMenuEnum.TempletType.values()) {
			templetTypeArr.add(new NameValueSelectDto(e.name, e.value.toString(), YsMenuEnum.TempletType.NONE.value.equals(e.value)));
		}
		
		List<NameValueSelectDto> statusArr = new ArrayList<NameValueSelectDto>();
		for (StatusEnum e : StatusEnum.values()) {
			statusArr.add(new NameValueSelectDto(e.name, e.value.toString(), e.value == StatusEnum.ON_USED.value));
		}

		Integer sortNo = sysMapper.getNextSortNoOfMenu(pid);
		String pname = pMenu.getName();
		Integer lev = pMenu.getLev() + 1;
		ToAddDto dto = new ToAddDto(pid, pname, lev, sortNo, templetTypeArr, statusArr);
		model.addAttribute("dto", dto);
		return "sys/ysMenu/toAdd";
	}
	@Data
	@AllArgsConstructor
	private static class ToAddDto{
		private Long pid; // 上级菜单ID
		private String pname; // 上级菜单名称
		private Integer lev; // 菜单等级
		private Integer sortNo; // 功能排序序号
		private List<NameValueSelectDto> templetTypeArr; // 模板类型
		private List<NameValueSelectDto> statusArr; // 状态
	}
	
	/*
	 * 调整顺序
	 */
	@GetMapping("/toMove")
	public String toMove(Model model, HttpServletRequest request, HttpServletResponse response,
			Long id){

		YsMenu entity = new YsMenu().setId(id).queryFirst();
		if (entity.getLev() == YsMenuEnum.Lev.ONE.value) {
			return W.error(model, "一级菜单不能移动");
		}
		
		List<YsMenu> listYsMenu = new YsMenu().setLev(entity.getLev() - 1).queryList();
		List<NameValueSelectDto> pidArr = new ArrayList<NameValueSelectDto>();
		for (YsMenu e : listYsMenu) {
			pidArr.add(new NameValueSelectDto(e.getName(), e.getId().toString(), e.getId().longValue() == entity.getPid().longValue()));
		}
		String name = entity.getName();
		ToMoveDto dto = new ToMoveDto(id, name, pidArr);
		model.addAttribute("dto", dto);
		return "sys/ysMenu/toMove";
	}
	@Data
	@AllArgsConstructor
	private static class ToMoveDto{
		private Long id; // 菜单ID
		private String name; // 菜单名称
		private List<NameValueSelectDto> pidArr; // 模板类型
	}
	
	/*
	 * 调整顺序
	 */
	@GetMapping("/toResort")
	public String toResort(Model model, HttpServletRequest request, HttpServletResponse response,
			Long pid){
		
		return ysMenuService.toResort(model, pid);
	}
	
	/**
	 * 设置基本信息
	 */
	@GetMapping("/toSettingBase")
	public String toSettingBase(Model model, Long id){
		return ysMenuService.toSettingBase(model, id);
	}
	
	/**
	 * 设置主表格
	 */
	@GetMapping("/toSettingGrid")
	public String toSettingGrid(Model model, Long id){
		if (id == null) {
			return W.error(model, "菜单ID不能为空");
		}
		model.addAttribute("id", id);
		return "sys/ysMenu/toSettingGrid";
	}
	
	/**
	 * 设置表单
	 */
	@GetMapping("/toSettingFormAddAndEdit")
	public String toSettingFormAddAndEdit(Model model, Long id){
		if (id == null) {
			return W.error(model, "菜单ID不能为空");
		}
		model.addAttribute("id", id);
		return "sys/ysMenu/toSettingFormAddAndEdit";
	}
	
	/**
	 * 设置详情
	 */
	@GetMapping("/toSettingFormDetail")
	public String toSettingFormDetail(Model model, Long id){
		if (id == null) {
			return W.error(model, "菜单ID不能为空");
		}
		model.addAttribute("id", id);
		return "sys/ysMenu/toSettingFormDetail";
	}


	/**
	 * 设置手机端表单
	 */
	@GetMapping("/toSettingFormApp")
	public String toSettingFormApp(Model model, Long id){
		if (id == null) {
			return W.error(model, "菜单ID不能为空");
		}
		model.addAttribute("id", id);
		return "sys/ysMenu/toSettingFormApp";
	}

	/*
	 * 调整导入字段顺序
	 */
	@GetMapping("/toResortImport")
	public String toResortImport(Model model, HttpServletRequest request, HttpServletResponse response,
			Long id){
		if (id == null) {
			return W.error(model, "菜单ID不能为空");
		}
		model.addAttribute("id", id);
		return "sys/ysMenu/toResortImport";
	}
	
	/**
	 * 设置导入字段
	 */
	@GetMapping("/toSettingImport")
	public String toSettingImport(Model model, Long id){
		if (id == null) {
			return W.error(model, "菜单ID不能为空");
		}
		model.addAttribute("id", id);
		return "sys/ysMenu/toSettingImport";
	}
	
	/**
	 * 设置按钮
	 */
	@GetMapping("/toSettingButton")
	public String toSettingButton(Model model, Long id){
		if (id == null) {
			return W.error(model, "菜单ID不能为空");
		}
		model.addAttribute("id", id);
		return "sys/ysMenu/toSettingButton";
	}
	
	/**
	 * 设置子列表字段
	 */
	@GetMapping("/toSettingSubGrid")
	public String toSettingSubGrid(Model model, Long id){
		if (id == null) {
			return W.error(model, "菜单ID不能为空");
		}
		YsMenu ysMenu = new YsMenu().setId(id).queryFirst();
		Long gridId = ysMenu.getGridId();
		if (gridId == null || gridId.longValue() == 0L) {
			return W.error(model, "暂未配置子列表ID");
		}
		model.addAttribute("id", id);
		model.addAttribute("gridId", gridId);
		return "sys/ysMenu/toSettingSubGrid";
	}




	/**
	 * 设置手机端子列表字段
	 */
	@GetMapping("/toSettingSubGridApp")
	public String toSettingSubGridApp(Model model, Long id){
		if (id == null) {
			return W.error(model, "菜单ID不能为空");
		}
		YsMenu ysMenu = new YsMenu().setId(id).queryFirst();
		Long gridId = ysMenu.getGridId();
		if (gridId == null || gridId.longValue() == 0L) {
			return W.error(model, "暂未配置子列表ID");
		}
		model.addAttribute("id", id);
		model.addAttribute("gridId", gridId);
		return "sys/ysMenu/toSettingSubGridApp";
	}
	
	/**
	 * 设置子列表按钮
	 */
	@GetMapping("/toSettingSubGridBtn")
	public String toSettingSubGridBtn(Model model, Long id){
		if (id == null) {
			return W.error(model, "菜单ID不能为空");
		}
		YsMenu ysMenu = new YsMenu().setId(id).queryFirst();
		model.addAttribute("id", id);
		model.addAttribute("gridId", ysMenu.getGridId() != null ? ysMenu.getGridId() : -1L);
		return "sys/ysMenu/toSettingSubGridBtn";
	}
	
	/*
	 * 批量新增字段
	 */
	@GetMapping("/toBatchAddCol")
	public String toBatchAddCol(Model model, Long id, String tableName){
		if (id == null) {
			return W.error(model, "菜单ID不能为空");
		}
		if (tableName == null) {
			return W.error(model, "表名不能为空");
		}
		
		/*
		 *  字段详情数组
		 *  1、已有的字段
		 *  2、未有的字段
		 */
		List<ToBatchAddColDto.RowDto> rows = new ArrayList<ToBatchAddColDto.RowDto>();
		
		List<Map<String, Object>> listDetail = sysMapper.listSysCol(tableName);
		for(Map<String, Object> detail : listDetail) {
			String code = StringUtil.firstCharacterToLower(detail.get("code").toString());
			if ("id".equals(code)) {
				continue;
			}
			String name = detail.containsKey("name") ? detail.get("name").toString() : "待完善名称";
			rows.add(new ToBatchAddColDto.RowDto(id, code, name));
		}
		
		ToBatchAddColDto dto = new ToBatchAddColDto(rows);
		model.addAttribute("sonlist", dto);
		
		return "sys/ysMenu/toBatchAddCol";
	}
	@Data
	@AllArgsConstructor
	public static class ToBatchAddColDto{
		private List<RowDto> rows; // 

		@Data
		@AllArgsConstructor
		public static class RowDto{
			private Long menuId;
			private String code;
			private String name;
		}
	}
	
	/**
	 * 设置行单击列表字段
	 */
	@GetMapping("/toSettingRowClickGrid")
	public String toSettingRowClickGrid(Model model, Long id){
		if (id == null) {
			return W.error(model, "菜单ID不能为空");
		}
		YsMenu ysMenu = new YsMenu().setId(id).queryFirst();
		Long rowClickGridId = ysMenu.getRowClickGridId();
		if (rowClickGridId == null || rowClickGridId.longValue() == 0L) {
			return W.error(model, "暂未配置行单击列表ID");
		}
		model.addAttribute("id", id);
		model.addAttribute("rowClickGridId", rowClickGridId);
		return "sys/ysMenu/toSettingRowClickGrid";
	}
	
	/**
	 * 设置全部
	 */
	@GetMapping("/toSettingAll")
	public String toSettingAll(Model model, Long id){
		if (id == null) {
			return W.error(model, "菜单ID不能为空");
		}
		
		YsMenu ysMenu = new YsMenu().setId(id).queryFirst();
		model.addAttribute("id", id);
		model.addAttribute("name", ysMenu.getName());
		return "sys/ysMenu/toSettingAll";
	}
	
	/**
	 * 生成表单HTML代码
	 */
	@GetMapping("/toGenerateFormHTML")
	public String toGenerateFormHTML(Model model, String ids){
		
		model.addAttribute("ids", ids);
		
		return "sys/ysMenu/toGenerateFormHTML";
	}
	
	/**
	 * 生成表单HTML代码
	 */
	@GetMapping("/test")
	public String test(Model model){
		
		return "sys/ysMenu/test";
	}
}
